<?php

/**
 *   OPENSHOP_taobao 颜色属性相关设置
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: api_tb_color.php,v 1.0 2009/11/5 07:35:58 add $
 *   @author      winner <jsycywm@hotmail.com>
 */

define('IN_OS', true);

require_once(dirname(__FILE__).'/includes/init.php');
$exc = new exchange($os->table('tb_guige1'),$db,'color_id','color_name');
	
/* act操作项的初始化 */
if (empty($_REQUEST['act'])){
    $_REQUEST['act'] = 'list';
}else{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

if($_REQUEST['act']=='list')
{	
	//检查权限
	admin_priv();
	
	$smarty->assign('ur_here',      '淘宝宝贝颜色关联');   
	//$smarty->assign('action_link',  array('href' => 'color.php?act=add', 'text' => '添加颜色'));
	$color_list = get_tb_colorlist();  
			
	$smarty->assign('color_list',   $color_list['color']);
    $smarty->assign('filter',       $color_list['filter']);
    $smarty->assign('record_count', $color_list['record_count']);
    $smarty->assign('page_count',   $color_list['page_count']);		 
	$smarty->assign('full_page',    1); 
    assign_query_info();    
	$smarty->display('api_tb_color_list.tpl');	
} elseif ($_REQUEST['act'] == 'remove'){ 
}elseif ($_REQUEST['act'] == 'query'){
    $color_list = get_tb_colorlist(); 
    $smarty->assign('color_list',   $color_list['color']);
    $smarty->assign('filter',       $color_list['filter']);
    $smarty->assign('record_count', $color_list['record_count']);
    $smarty->assign('page_count',   $color_list['page_count']);

    make_json_result($smarty->fetch('api_tb_color_list.tpl'), '',
        array('filter' => $color_list['filter'], 'page_count' => $color_list['page_count']));
}elseif($_REQUEST['act']=='relevancy'){
 
    $smarty->assign('ur_here',      '淘宝宝贝颜色关联设置'); 
	$smarty->assign('action_link',  array('href' => 'api_tb_color.php?act=list', 'text' => '淘宝宝贝颜色管理'));
	 
	$vid =  intval($_GET['vid'] ) ;
	$cid =  intval($_GET['cid'] ) ;
	$tb_color_name = $db->getOne("SELECT name FROM ".$os->table('tb_guige1')." WHERE vid='".$vid."'"); 
	$itemcat_name = $db->getOne("SELECT name FROM ".$os->table('tb_itemcat')." WHERE cid='".$cid."'"); 
	$smarty->assign('cat_color_list',get_tb_cat_color_list( $vid,$cid ));
	$smarty->assign('vid',$vid);  
	$smarty->assign('cid',$cid); 
	$smarty->assign('cname','['.$cid.']'.$itemcat_name);   
	$smarty->assign('vname','['.$vid.']'.$tb_color_name);  
	assign_query_info();
	$smarty->display('api_tb_color_relevancy.tpl');	
} elseif($_REQUEST['act'] == 'set_default_colorlib'){  
}elseif($_REQUEST['act']=='search_cat'){   //查找关联的
    /* 检查权限 */ 
    include_once(ROOT_PATH . 'includes/cls_json.php'); 
    $json   = new JSON;
    $filter = $json->decode($_GET['JSON']);
    $filter->keyword = json_str_iconv($filter->keyword);	
     
    $sql = "SELECT color_code AS id, CONCAT('[',color_code,']', color_name) as name FROM " . $os->table('color') .
        " WHERE color_name LIKE '%" . mysql_like_quote($filter->keyword) . "%' or color_code LIKE '%" . mysql_like_quote($filter->keyword) . "%' LIMIT 50";
	 
    $arr = $db->getAll($sql);
    if (empty($arr)){
        $arr = array(0 => array(
            'id'   => 0,
            'name' => '没有找到相应记录，请重新搜索'
        ));
    }

    make_json_result($arr);	
    
}elseif ($_REQUEST['act']=='add_cat_color'){
	 
    include_once(ROOT_PATH . 'includes/cls_json.php');    
    $json = new JSON; 
    $color_code =$json->decode(  $_GET['add_ids'] );
    $color_code =$color_code[0] ; 
    $arguments  = $json->decode($_GET['JSON']);
    $vid   = $arguments[0];
    $cid   = $arguments[1];
	$sql = "update " . $os->table('tb_guige1') . " set color_code='".$color_code."'where vid=". $vid;
	$db->query($sql);  
    $arr = get_tb_cat_color_list($vid,$cid );  
    $opt = array(); 
    foreach ($arr AS $val)
    {
        $opt[] = array('value'      => $val['color_code'],
                        'text'      => '['.$val['color_code'].']'.$val['color_name'],
                        'data'      => '');
    }
    
    clear_cache_files();
    
    make_json_result($opt);
    
}elseif($_REQUEST['act']=='drop_cat_color'){
	
	
	include_once(ROOT_PATH . 'includes/cls_json.php');
	$json = new JSON;

	$color_code =$json->decode(  $_GET['add_ids'] );
	$color_code =$color_code[0] ; 
	$arguments  = $json->decode($_GET['JSON']);
	$vid   = $arguments[0];
	$cid   = $arguments[1];
	$color_name = $db->getOne("SELECT color_name FROM " . $os->table('color') . " WHERE color_id='$color_id'");  
	$sql = "update " . $os->table('tb_guige1') . " set color_code='' where vid=". $vid; 
	$db->query($sql); 
	$arr = get_tb_cat_color_list($color_id,$cid);
	$opt = array(); 
	foreach ($arr AS $val){
		$opt[] = array('value'      => $val['cat_id'],
			'text'      => $val['cat_name'],
			'data'      => '');
	}
	
	clear_cache_files();
	make_json_result($opt);	
}elseif($_REQUEST['act']=='auto_relevancy'){
	$sql = "update " . $os->table('tb_guige1') . " as tb_c,".$os->table('color')." as c set tb_c.color_code=c.color_code   where tb_c.vid=c.vid;"; 
	$db->query($sql); 
	
	$link[0]['text'] = '颜色关联';
	$link[0]['href'] = 'api_tb_color.php?act=list';
	$link[0]['flag'] = 'api_tb_color';
	sys_msg('关联成功！', 0, $link);
	clear_cache_files();
	
}
	

function get_tb_colorlist(){
	global $_CFG;
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();

        $filter['keyword']  = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);

        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
        }        
        
        if ($filter['keyword'])
        {
			$where .= " AND (tb_gg1.vid LIKE '%" . mysql_like_quote($filter['keyword']) . "%' 
						OR tb_gg1.name LIKE '%" . mysql_like_quote($filter['keyword']) . "%'
						OR tb_gg1.cid LIKE '%" . mysql_like_quote($filter['keyword']) . "%'
				        OR tb_ic.name LIKE '%" . mysql_like_quote($filter['keyword']) . "%'
						)";
        }     
                
        /* 记录总数以及页数 */
        $sql = "SELECT COUNT(*) FROM ".$GLOBALS['os']->table('tb_guige1').' as tb_gg1 
        	    left join open_tb_itemcat as tb_ic on tb_ic.cid =tb_gg1.cid
        		where 1 '.$where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

         /* 查询记录 */ 
		$sql = "select tb_gg1.vid, tb_gg1.cid,tb_ic.name as icname ,tb_gg1.pid,tb_gg1.prop_name,tb_gg1.name,tb_gg1.color_code ,c.color_name
				from open_tb_guige1 as tb_gg1 
				left join open_tb_itemcat as  tb_ic on tb_gg1.cid =tb_ic.cid  
				left join open_color as  c on tb_gg1.color_code =c.color_code  where 1 ".$where." ORDER BY  tb_gg1.id DESC";  
        /* 查询记录 */ 
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    } 
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);
    
    $arr = array();
    while ($rows = $GLOBALS['db']->fetchRow($res))
    {  
        $arr[] = $rows;
    }	
    return array('color' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}



function get_tb_cat_color_list($vid=0,$cid=0){ 
	static $rows = NULL;  
    $sql ="select tb_gg1.cid,tb_itemcat.name,tb_gg1.vid,tb_gg1.color_code,c.color_name from open_tb_guige1 as tb_gg1 
			left join open_color as  c  on c.color_code =tb_gg1.color_code
			left join open_tb_itemcat as tb_itemcat on tb_itemcat.cid =tb_gg1.cid "; 

	if($vid>0){
		$sql .= " WHERE tb_gg1.vid = '$vid' and tb_gg1.cid = '$cid' ";
	}
	$sql .= " ORDER BY tb_gg1.id DESC";   
	if ($rows === NULL){
		$rows = $GLOBALS['db']->getAll($sql);
	}
	return $rows;		
}
?>